package www.geek.filemanager.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import www.geek.filemanager.R;
import www.geek.filemanager.dialog.CustomDialog;


public class    ToastUtils {

    public static final int TOAST_SUCCESS = 0;
    public static final int TOAST_LOADING = 1;
    public static final int TOAST_WARNNING = 2;

    //=============单例toast用到===============
    private static String oldMsg;
    private static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;

    public static void showToastWithProgress(Context context, String text, int duration) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);

        LinearLayout toastView = (LinearLayout) toast.getView();
        toastView.setOrientation(LinearLayout.HORIZONTAL);
        ImageView imageCode = new ImageView(context);

    }

    public static Dialog showToastDialog(Context context, String text, int type, boolean isCanncel, final OnClickListener onCloseListener) {
        final CustomDialog dialog = new CustomDialog(context, R.layout.layout_loading_toast, R.style.CustomDialog);
        ImageView leftImg = (ImageView) dialog.findViewById(R.id.show_img);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelOnBackClick(isCanncel);
        dialog.setCancelable(false);
        TextView rightTv = (TextView) dialog.findViewById(R.id.show_text);
        dialog.findViewById(R.id.close_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onCloseListener != null) {
                    onCloseListener.onClick(v);
                }
            }
        });

        if (type == TOAST_SUCCESS) {
            leftImg.setImageResource(R.drawable.icon_success);
        } else if (type == TOAST_LOADING) {
            AnimationDrawable animationDrawable = null;
            leftImg.setImageResource(R.drawable.icon_loading_anim);
            animationDrawable = (AnimationDrawable) leftImg.getDrawable();
            animationDrawable.start();
        } else {
            leftImg.setImageResource(R.drawable.icon_warnning);
        }

        rightTv.setText(text);
        dialog.show();
        return dialog;
    }

    public static Dialog showToastDialogNoClose(Context context, String text, int type, boolean isCanncel) {
        Dialog dialog = showToastDialog(context, text, type, isCanncel, null);
        dialog.findViewById(R.id.close_btn).setVisibility(View.GONE);
        return dialog;
    }

    /**
     * show short toast
     *
     * @param context
     * @param text
     */
    public static void showToastShort(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * show long toast
     *
     * @param context
     * @param text
     */
    public static void showToastLong(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }


    /**
     * show Single Toast
     *
     * @param context
     * @param s
     */
    public static void showToastSingle(Context context, String s) {
        if (toast == null) {
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > 500) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }


}
